package edu.ptu.acl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.ptu.acl.common.MyConstants;
import edu.ptu.acl.common.ResultVO;
import edu.ptu.acl.dto.param.RoleModuleParam;
import edu.ptu.acl.dto.param.RoleParam;
import edu.ptu.acl.entity.Role;
import edu.ptu.acl.service.IRoleService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;
import javax.validation.constraints.Pattern;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LinJC
 * @since 2022-03-15
 */
@RestController
@RequestMapping("/role")
@Validated
public class RoleController {

    @Resource
    private IRoleService roleService;

    //根据角色模块获取角色信息
    @GetMapping("/getRoleByRoleModuleId/{id}/{pageNo}/{pageSize}")
    public ResultVO getRoleByRoleModuleId(@PathVariable @Min(value = 10000000000000000L, message = "id不规范") Long id,
                                          @PathVariable @Min(1) Integer pageNo, @PathVariable @Min(1) Integer pageSize) {
        Page<Role> roles = new Page<>(pageNo, pageSize);
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("parent_id", id);
        roleService.page(roles, roleQueryWrapper);
        return ResultVO.success(roles);
    }

    //新增角色信息
    @PostMapping("/saveRole")
    public ResultVO saveRole(@RequestBody @Validated(RoleParam.Save.class) RoleParam roleParam) {
        roleService.validateName(roleParam.getParentId(),roleParam.getId(),roleParam.getName());
        Role role = Role.builder().name(roleParam.getName()).parentId(roleParam.getParentId())
                .status(roleParam.getStatus()).remark(roleParam.getRemark()).build();
        roleService.save(role);
        return ResultVO.success();
    }

    //编辑角色信息
    @PutMapping("/updateRole")
    public ResultVO updateRole(@RequestBody @Validated(RoleParam.Update.class) RoleParam roleParam) {
        roleService.validateName(roleParam.getParentId(),roleParam.getId(),roleParam.getName());
        Role role = Role.builder().id(roleParam.getId()).name(roleParam.getName()).parentId(roleParam.getParentId())
                .status(roleParam.getStatus()).remark(roleParam.getRemark())
                .version(roleParam.getVersion()).build();
        if (roleService.updateById(role)) {
            return ResultVO.success();
        }
        return ResultVO.fail(MyConstants.ResultCode.OPTIMISTIC_LOCKING);
    }

    //冻结角色信息freezeRole
    @PutMapping("/freezeRole/{id}/{status}")
    public ResultVO updateRoleStatus(@PathVariable @Min(value = 10000000000000000L, message = "id不规范") Long id,
                                     @PathVariable @Pattern(regexp = "[10]",message = "状态异常") String status) {
        UpdateWrapper<Role> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status",status);
        updateWrapper.eq("id",id);
        roleService.update(updateWrapper);
        return ResultVO.success();
    }

    //删除角色信息
    @DeleteMapping("/deleteRole/{id}")
    public ResultVO deleteRole(@PathVariable @Min(value = 10000000000000000L, message = "id不规范") Long id) {

        roleService.deleteRole(id);
        return ResultVO.success();
    }


}
